package org.study.javafx.study01.menu.code;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import org.study.javafx.study01.AppStarter;
import org.study.javafx.study01.config.AppConfig;

/**
 * 参考 https://blog.csdn.net/qq_43750882/article/details/110144285
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年08月18日
 */
@Slf4j
public class MenuCode2 extends Application {
    /**
     * 直接运行此方法会报如下错误：
     * 错误: 缺少 JavaFX 运行时组件, 需要使用该组件来运行此应用程序
     * 请运行 {@link AppStarter#main(String[])}
     */
    public static void main(String[] args) {
        AppConfig.init();
        launch(args);
    }

    @Override
    public void init() throws Exception {
        super.init();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color:#7AC5CD");

        MenuBar menuBar = new MenuBar();
        Menu menu1 = new Menu("menu1");
        Menu menu2 = new Menu("menu2");
        Menu menu3 = new Menu("menu3");
        Menu menu4 = new Menu("menu4");

        // 朴实无华的分割线
        SeparatorMenuItem separ1 = new SeparatorMenuItem();
        SeparatorMenuItem separ2 = new SeparatorMenuItem();

        MenuItem item1 = new MenuItem("MenuItem1");
        MenuItem item2 = new MenuItem("MenuItem2");
        MenuItem item3 = new MenuItem("MenuItem3");
        MenuItem item4 = new MenuItem("MenuItem4");

        // 2级菜单，menu放在另一个menu下
        Menu m = new Menu("拆分Menu");
        MenuItem item5 = new MenuItem("MenuItem5");
        MenuItem item6 = new MenuItem("MenuItem6");
        MenuItem item7 = new MenuItem("MenuItem7");
        MenuItem item8 = new MenuItem("MenuItem8");
        m.getItems().addAll(item5, item6);

        // 单选按钮，单选按钮组
        ToggleGroup tg = new ToggleGroup();
        RadioMenuItem rmi1 = new RadioMenuItem("RadioMenuItem1");
        RadioMenuItem rmi2 = new RadioMenuItem("RadioMenuItem2");
        RadioMenuItem rmi3 = new RadioMenuItem("RadioMenuItem3");
        rmi1.setToggleGroup(tg);
        rmi2.setToggleGroup(tg);
        rmi3.setToggleGroup(tg);
        // 默认选择
        rmi1.setSelected(true);

        // 多选
        CheckMenuItem cmi1 = new CheckMenuItem("CheckMenuItem1");
        CheckMenuItem cmi2 = new CheckMenuItem("CheckMenuItem2");
        CheckMenuItem cmi3 = new CheckMenuItem("CheckMenuItem3");

        // 禁用
        item1.setDisable(true);
        menu4.setDisable(true);

        // 隐藏-不可见
        item7.setVisible(false);

        // 单击事件
        rmi1.setOnAction(event -> {
            System.out.println("\nrmi1当前选择状态 = " + rmi1.isSelected());
            System.out.println("rmi2当前选择状态 = " + rmi2.isSelected());
            System.out.println("rmi3当前选择状态 = " + rmi3.isSelected());
        });
        rmi2.setOnAction(event -> {
            System.out.println("\nrmi1当前选择状态 = " + rmi1.isSelected());
            System.out.println("rmi2当前选择状态 = " + rmi2.isSelected());
            System.out.println("rmi3当前选择状态 = " + rmi3.isSelected());
        });
        rmi3.setOnAction(event -> {
            System.out.println("\nrmi1当前选择状态 = " + rmi1.isSelected());
            System.out.println("rmi2当前选择状态 = " + rmi2.isSelected());
            System.out.println("rmi3当前选择状态 = " + rmi3.isSelected());
        });

        // 添加
        menu1.getItems().addAll(item1, separ1, item2, separ2, item3, item4, m, item7, item8);
        menu2.getItems().addAll(rmi1, rmi2, rmi3);
        menu3.getItems().addAll(cmi1, cmi2, cmi3);

        // menu1.setOnShowing();menu1.setOnShown();menu1.setOnHiding();menu1.setOnHidden();
        menuBar.getMenus().addAll(menu1, menu2, menu3, menu4);

        an.getChildren().addAll(menuBar);

        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX");
        primaryStage.setWidth(500);
        primaryStage.setHeight(300);
        primaryStage.show();

        menuBar.setPrefWidth(primaryStage.getWidth());

        // 添加根节点的宽度监听，使得菜单栏宽度和窗口保持一致
        an.widthProperty().addListener((observable, oldValue, newValue) -> menuBar.setPrefWidth(newValue.doubleValue()));
    }

    @Override
    public void stop() throws Exception {
        super.stop();
    }
}
